package com.composetest

import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.composetest.ui.MyApp
import com.composetest.ui.theme.ComposeTestTheme
import com.google.accompanist.systemuicontroller.rememberSystemUiController

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeTestTheme {
                //SplashRoute()
                val systemUiController = rememberSystemUiController()
                SideEffect {
                    // color 状态栏颜色,darkIcons 的值控制状态栏显示的图标字体样式
                    systemUiController.setSystemBarsColor( color = Color.White, darkIcons = true)
                }

                MyApp()
            }
        }
    }
}

class MessageBean {

    companion object {
        fun getData(): List<MessageBean> {
            return listOf(
                MessageBean("jin", "到目前为止，"),
                MessageBean("jin2", "到目前为止，我们只有一个消息的卡片，"),
                MessageBean("jin3", "到目前为止，我们只有一个消息的卡片，看上去有点单调，"),
                MessageBean(
                    "jin4",
                    "到目前为止，我们只有一个消息的卡片，看上去有点单调，所以让我们来改善它，"
                ),
                MessageBean(
                    "jin5",
                    "到目前为止，我们只有一个消息的卡片，看上去有点单调，所以让我们来改善它，让它拥有多条信息。"
                ),
                MessageBean(
                    "jin6",
                    "到目前为止，我们只有一个消息的卡片，看上去有点单调，所以让我们来改善它，"
                ),
                MessageBean("jin7", "到目前为止，我们只有一个消息的卡片，看上去有点单调，"),
                MessageBean("jin8", "到目前为止，我们只有一个消息的卡片，"),
            )
        }
    }

    var title: String
    var content: String

    constructor(title: String, content: String) {
        this.title = title
        this.content = content
    }
}

@Composable
fun MessageCord(messageBean: MessageBean) {

    var isExpanded by remember {
        mutableStateOf(false)
    }

    val surfaceColor by animateColorAsState(
        targetValue = if (isExpanded) Color(0xFFCCCCCC) else MaterialTheme.colorScheme.background
    )

    Surface(
        modifier = Modifier
            .padding(all = 8.dp)
            .clickable { isExpanded = !isExpanded },
        color = surfaceColor,
        shape = MaterialTheme.shapes.medium,
        shadowElevation = 5.dp,
    ) {
        Row(modifier = Modifier.padding(16.dp)) {
            Image(
                painterResource(id = R.drawable.ic_launcher_background),
                contentDescription = null,
                modifier = Modifier
                    .size(50.dp)
                    .clip(CircleShape)
                    .border(
                        1.5.dp,
                        MaterialTheme.colorScheme.secondary,
                        shape = CircleShape
                    ) // 添加边框
            )
            Spacer(modifier = Modifier.padding(horizontal = 8.dp))
            Column() {
                Text(
                    text = messageBean.title,
                    color = MaterialTheme.colorScheme.primary,
                    style = MaterialTheme.typography.titleMedium
                )
                Spacer(modifier = Modifier.padding(vertical = 4.dp))
                Text(
                    text = messageBean.content,
                    style = MaterialTheme.typography.bodyMedium,
                    maxLines = if (isExpanded) Int.MAX_VALUE else 1,
                    modifier = Modifier.animateContentSize()
                )
            }
        }
    }
}

@Composable
fun Conversation(messages: List<MessageBean>) {
    LazyColumn {
        items(messages) { message ->
            MessageCord(messageBean = message)
        }
    }
}

@Preview(name = "Light Mode")
@Preview(
    uiMode = Configuration.UI_MODE_NIGHT_YES,
    showBackground = true,
    name = "Dark Mode"
)
@Composable
fun GreetingPreview() {
    ComposeTestTheme {
//        MessageCord(MessageBean("jin", "lianzai"))
        Conversation(MessageBean.getData())
    }
}